/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view;

import java.awt.Container;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.NumberFormat;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import soccerscope.model.ClientScene;
import soccerscope.model.Param;
import soccerscope.model.Player;
import soccerscope.model.SoccerObjectID;
import soccerscope.model.Team;
import soccerscope.model.agentplan.CommentDB;
import soccerscope.util.geom.Point2f;
import soccerscope.util.geom.Tuple2f;

public class InformationWindow extends JFrame implements ScopeWindow,
		SoccerObjectID, SoccerObjectSelectObserver, MouseListener {

	private static final long serialVersionUID = 3926358716905826321L;

	public final static String title = "information";

	private ClientScene clientScene;

	private Container container = null;

	private JTabbedPane tabbedPane;

	private int leftNumber;

	private int rightNumber;

	private NumberFormat nf;

	private String tuple2String(Tuple2f tuple) {
		return "(" + this.nf.format(tuple.x) + ", " + this.nf.format(tuple.y)
				+ ")";
	}

	public InformationWindow() {
		// frame title
		super("information");

		// �����ѿ�ν��
		this.leftNumber = LEFT_1;
		this.rightNumber = RIGHT_1;
		this.nf = NumberFormat.getInstance();
		this.nf.setMaximumFractionDigits(2);

		// �%ߡ��ʥ�����
		this.clientScene = ClientScene.createScene();

		// get content pane
		this.container = this.getContentPane();

		this.tabbedPane = new JTabbedPane();
		this.tabbedPane.addTab("Left", createLeftPlayerPanel());
		this.tabbedPane.addTab("Right", createRightPlayerPanel());
		this.tabbedPane.addTab("Other", createOtherPanel());
		this.container.add(this.tabbedPane);

		pack();
		setResizable(false);
	}

	private PlayerButton lpButton;

	private JLabel lpos;

	private JLabel lvel;

	private JLabel lacc;

	private JLabel langle;

	private JLabel lneck;

	private JLabel lvisible;

	private JTextField lmessage;

	private JTextArea lcomment;

	private JPanel createLeftPlayerPanel() {
		JPanel jp = new JPanel();
		jp.setLayout(new BoxLayout(jp, BoxLayout.X_AXIS));
		JPanel jpw = new JPanel();
		jpw.setLayout(new BoxLayout(jpw, BoxLayout.Y_AXIS));
		JPanel jpc = new JPanel();
		jpc.setLayout(new BoxLayout(jpc, BoxLayout.Y_AXIS));
		JPanel jpe = new JPanel();
		jpe.setLayout(new BoxLayout(jpe, BoxLayout.Y_AXIS));
		jp.add(jpw);
		jp.add(Box.createHorizontalStrut(4));
		jp.add(jpc);
		jp.add(Box.createHorizontalStrut(16));
		jp.add(jpe);
		this.lpButton = new PlayerButton(new Player(Team.LEFT_SIDE));
		this.lpButton.addMouseListener(this);
		JPanel pButtonPanel = new JPanel();
		pButtonPanel.add(this.lpButton);
		jpw.add(pButtonPanel);
		this.lpos = new JLabel(new Point2f().toString());
		this.lvel = new JLabel(new Point2f().toString());
		this.lacc = new JLabel(new Point2f().toString());
		jpc.add(new JLabel("Position"));
		jpc.add(this.lpos);
		jpc.add(new JLabel("Velocity"));
		jpc.add(this.lvel);
		jpc.add(new JLabel("Acceleration"));
		jpc.add(this.lacc);
		this.langle = new JLabel("0");
		this.lneck = new JLabel("0");
		this.lvisible = new JLabel("0");
		jpe.add(new JLabel("BodyAngle"));
		jpe.add(this.langle);
		jpe.add(new JLabel("NeckAngle"));
		jpe.add(this.lneck);
		jpe.add(new JLabel("VisibleAngle"));
		jpe.add(this.lvisible);

		JPanel leftplayerPanel = new JPanel();
		leftplayerPanel.setLayout(new BoxLayout(leftplayerPanel,
				BoxLayout.Y_AXIS));
		this.lmessage = new JTextField();
		this.lcomment = new JTextArea(4, 0);
		JScrollPane jsp = new JScrollPane(this.lcomment);
		leftplayerPanel.add(jp);
		leftplayerPanel.add(this.lmessage);
		leftplayerPanel.add(jsp);
		return leftplayerPanel;
	}

	private PlayerButton rpButton;

	private JLabel rpos;

	private JLabel rvel;

	private JLabel racc;

	private JLabel rangle;

	private JLabel rneck;

	private JLabel rvisible;

	private JTextField rmessage;

	private JTextArea rcomment;

	private JPanel createRightPlayerPanel() {
		JPanel jp = new JPanel();
		jp.setLayout(new BoxLayout(jp, BoxLayout.X_AXIS));
		JPanel jpw = new JPanel();
		jpw.setLayout(new BoxLayout(jpw, BoxLayout.Y_AXIS));
		JPanel jpc = new JPanel();
		jpc.setLayout(new BoxLayout(jpc, BoxLayout.Y_AXIS));
		JPanel jpe = new JPanel();
		jpe.setLayout(new BoxLayout(jpe, BoxLayout.Y_AXIS));
		jp.add(jpw);
		jp.add(Box.createHorizontalStrut(4));
		jp.add(jpc);
		jp.add(Box.createHorizontalStrut(16));
		jp.add(jpe);
		this.rpButton = new PlayerButton(new Player(Team.RIGHT_SIDE));
		this.rpButton.addMouseListener(this);
		JPanel pButtonPanel = new JPanel();
		pButtonPanel.add(this.rpButton);
		jpw.add(pButtonPanel);
		this.rpos = new JLabel(new Point2f().toString());
		this.rvel = new JLabel(new Point2f().toString());
		this.racc = new JLabel(new Point2f().toString());
		jpc.add(new JLabel("Position"));
		jpc.add(this.rpos);
		jpc.add(new JLabel("Velocity"));
		jpc.add(this.rvel);
		jpc.add(new JLabel("Acceleration"));
		jpc.add(this.racc);
		this.rangle = new JLabel("0");
		this.rneck = new JLabel("0");
		this.rvisible = new JLabel("0");
		jpe.add(new JLabel("BodyAngle"));
		jpe.add(this.rangle);
		jpe.add(new JLabel("NeckAngle"));
		jpe.add(this.rneck);
		jpe.add(new JLabel("VisibleAngle"));
		jpe.add(this.rvisible);

		JPanel rightplayerPanel = new JPanel();
		rightplayerPanel.setLayout(new BoxLayout(rightplayerPanel,
				BoxLayout.Y_AXIS));
		this.rmessage = new JTextField();
		this.rcomment = new JTextArea(4, 0);
		JScrollPane jsp = new JScrollPane(this.rcomment);
		rightplayerPanel.add(jp);
		rightplayerPanel.add(this.rmessage);
		rightplayerPanel.add(jsp);
		return rightplayerPanel;
	}

	private JLabel ballpos;

	private JLabel ballvel;

	private JLabel ballacc;

	private JPanel createOtherPanel() {
		JPanel otherPanel = new JPanel();
		otherPanel.setLayout(new BoxLayout(otherPanel, BoxLayout.Y_AXIS));
		otherPanel.setBorder(new TitledBorder("Ball"));
		this.ballpos = new JLabel(new Point2f().toString());
		this.ballvel = new JLabel(new Point2f().toString());
		this.ballacc = new JLabel(new Point2f().toString());
		otherPanel.add(new JLabel("Position"));
		otherPanel.add(this.ballpos);
		otherPanel.add(new JLabel("Velocity"));
		otherPanel.add(this.ballvel);
		otherPanel.add(new JLabel("Acceleration"));
		otherPanel.add(this.ballacc);
		return otherPanel;
	}

	private void updatePane() {
		this.lpButton.setPlayer(this.clientScene.player[this.leftNumber]);
		this.lpos
				.setText(tuple2String(this.clientScene.player[this.leftNumber].pos));
		this.lvel
				.setText(tuple2String(this.clientScene.player[this.leftNumber].vel));
		this.lacc
				.setText(tuple2String(this.clientScene.player[this.leftNumber].acc));
		this.langle.setText(String
				.valueOf(this.clientScene.player[this.leftNumber].angle));
		this.lneck.setText(String
				.valueOf(this.clientScene.player[this.leftNumber].angleNeck));
		this.lvisible
				.setText(String
						.valueOf(this.clientScene.player[this.leftNumber].angleVisible));
		this.lmessage
				.setText(this.clientScene.player[this.leftNumber].sayMessage);
		this.lmessage.setCaretPosition(0);
		this.lcomment.setText(CommentDB.getComment(this.clientScene.time,
				this.leftNumber));
		this.rpButton.setPlayer(this.clientScene.player[this.rightNumber]);
		this.rpos
				.setText(tuple2String(this.clientScene.player[this.rightNumber].pos));
		this.rvel
				.setText(tuple2String(this.clientScene.player[this.rightNumber].vel));
		this.racc
				.setText(tuple2String(this.clientScene.player[this.rightNumber].acc));
		this.rangle.setText(String
				.valueOf(this.clientScene.player[this.rightNumber].angle));
		this.rneck.setText(String
				.valueOf(this.clientScene.player[this.rightNumber].angleNeck));
		this.rvisible
				.setText(String
						.valueOf(this.clientScene.player[this.rightNumber].angleVisible));
		this.rmessage
				.setText(this.clientScene.player[this.rightNumber].sayMessage);
		this.rmessage.setCaretPosition(0);
		this.rcomment.setText(CommentDB.getComment(this.clientScene.time,
				this.rightNumber));
		this.ballpos.setText(this.clientScene.ball.pos.toString());
		this.ballvel.setText(this.clientScene.ball.vel.toString());
		this.ballacc.setText(this.clientScene.ball.acc.toString());
	}

	public void setScene(ClientScene clientScene) {
		this.clientScene = clientScene;
		updatePane();
	}

	// �����Ѳ�����
	public void selectSoccerObject(int id, boolean flag) {
		if (id <= LEFT_11)
			this.leftNumber = id;
		else if (id <= RIGHT_11)
			this.rightNumber = id;
		updatePane();
	}

	// MouseListener
	public void mouseClicked(MouseEvent e) {
		Object o = e.getSource();
		// ������å�
		// ���ֹ�򥤥󥯥����
		if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
			if (o == this.lpButton) {
				this.leftNumber = (this.leftNumber + 1) % Param.MAX_PLAYER;
			} else if (o == this.rpButton) {
				this.rightNumber = (this.rightNumber + 1) % Param.MAX_PLAYER
						+ Param.MAX_PLAYER;
			}
		}
		// ������å�
		// ���ֹ��ǥ������
		else if (e.getModifiers() == InputEvent.META_MASK) {
			if (o == this.lpButton) {
				this.leftNumber = (this.leftNumber - 1 + Param.MAX_PLAYER)
						% Param.MAX_PLAYER;
			} else if (o == this.rpButton) {
				this.rightNumber = (this.rightNumber - 1) % Param.MAX_PLAYER
						+ Param.MAX_PLAYER;
			}
		}
		updatePane();
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}
}
